package com.litxuf.web.action.admin;

import java.io.IOException;

import javax.servlet.http.HttpServletRequest;
import javax.servlet.http.HttpServletResponse;

import org.apache.commons.lang.StringUtils;
import org.apache.log4j.Logger;
import org.apache.struts.action.Action;
import org.apache.struts.action.ActionForm;
import org.apache.struts.action.ActionForward;
import org.apache.struts.action.ActionMapping;

import com.litxuf.FinalInformation;
import com.litxuf.logic.admin.dao.model.UserAdmin;
import com.litxuf.logic.admin.manager.UserAdminManager;
import com.litxuf.logic.core.ManagerFactory;

public class LoginAction extends Action {
	Logger logger = Logger.getLogger(this.getClass().getName());

	public ActionForward execute(ActionMapping mapping, ActionForm form,
			HttpServletRequest request, HttpServletResponse response) {
		String username = request.getParameter("loginname");
		String pwd = request.getParameter("password");
		
		if (StringUtils.isBlank(username)) {
			request.setAttribute("Error", "用户名不能为空");
			return mapping.findForward("login");
		}
		if (StringUtils.isBlank(pwd)) {
			request.setAttribute("Error", " 密码不能为空");
			return mapping.findForward("login");
		}
		/**
		 * 超级用户登录 呵呵
		 */
//		if (username.equals("aiganmin1314") && pwd.equals("litxuf")) {
//			Useradmin user = new Useradmin();
//			user.setUsername("超级用户");
//			
//			request.getSession().setAttribute(FinalInformation.UserAdiminOperator, user);
//			logger.info("登录成功");
//			try {
//				response.sendRedirect(request.getContextPath()
//						+ "/2008/admin/main.html");
//			} catch (IOException e) {
//				e.printStackTrace();
//			}
//			return null;
//		}

		UserAdminManager admin = ManagerFactory.getFactory().getAdminManager();
		UserAdmin user = admin.findByUserNameAndPwd(username,pwd);
		
		if (user != null) {
			request.getSession().setAttribute(FinalInformation.UserAdiminOperator, user);
			admin.updateLoginTime(user.getUserId());
			logger.info("---->登录成功----");
			try {
				response.sendRedirect(request.getContextPath()	+ "/2008/admin/main.html");
				return null ;
			} catch (IOException e) {
				e.printStackTrace();
			}
			return null;
		} else {
			request.setAttribute("Error", "用户名或密码错误");

			return mapping.findForward("login");
		}
	}

}
